<template>
  <div class="SIMaintenance">
    <el-container>
      <el-header class="header">
        <el-button type="primary" @click="dialogFormVisibleImport=true">添加歌曲</el-button>
      </el-header>
      <el-main>
        <el-table :data="tableData" border fit stripe highlight-current-row style="width: 100%;">
          <el-table-column label="歌曲" align="center" width="180">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.musicName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="歌手" align="center" min-width="180">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.singer }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer>
        <el-pagination background layout="prev, pager, next" :total="total" :current-page.sync="currentPage" @current-change="currentChange"></el-pagination>
      </el-footer>
    </el-container>
    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="edit" :rules="rules">
        <el-form-item label="歌曲名称：" :label-width="formLabelWidth" prop="musicName">
          <el-input v-model="form.musicName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="歌手：" :label-width="formLabelWidth" prop="singer">
          <el-input v-model="form.singer" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="歌曲链接：" :label-width="formLabelWidth" prop="musicMp3">
          <el-input v-model="form.musicMp3" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片链接：" :label-width="formLabelWidth" prop="musicImg">
          <el-input v-model="form.musicImg" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editMusic('edit')">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="添加歌曲" :visible.sync="dialogFormVisibleImport">
      <el-form :model="form2" :rules="rules" ref="add">
        <el-form-item label="歌曲名称：" :label-width="formLabelWidth" prop="musicName">
          <el-input v-model="form2.musicName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="歌手：" :label-width="formLabelWidth" prop="singer">
          <el-input v-model="form2.singer" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="歌曲链接：" :label-width="formLabelWidth" prop="musicMp3">
          <el-input v-model="form2.musicMp3" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片链接：" :label-width="formLabelWidth" prop="musicImg">
          <el-input v-model="form2.musicImg" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleImport = false">取 消</el-button>
        <el-button type="primary" @click="addMusic('add')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getList, editMusic, addMusic, deltete } from '@/api/music'

export default {
  name: 'SIMaintenance',
  computed: {
    ...mapGetters([
      'name'
    ])
  },
   data() {
      return {
        tableData: [],
        currentPage: 1,
        dialogFormVisible: false,
        dialogFormVisibleImport: false,
        form: {
          id: "",
          musicName: "",
          singer: "",
          musicImg: "",
          musicMp3: "",
        },
        rules: {
          musicName: [
            { required: true, message: '请输入歌曲名字', trigger: 'blur' },
            { min: 1, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ],
          singer: [
            { required: true, message: '请输入歌手名字', trigger: 'blur' },
            { min: 1, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ],
          musicImg: [
            { required: true, message: '请填写图片链接', trigger: 'blur' }
          ],
          musicMp3: [
            { required: true, message: '请填写歌曲链接', trigger: 'blur' }
          ],
        },
        form2: {
          musicName: "",
          singer: "",
          musicMp3: '',
          musicImg: "",
        },
        formLabelWidth: '120px',
        total: 0,
        fileList: [],
      }
  },
  methods: {
    handleEdit(index, row) {
      this.dialogFormVisible = true;
      this.form.id = row.id;
      this.form.musicName = row.musicName;
      this.form.singer = row.singer;
      this.form.musicImg = row.musicImg;
      this.form.musicMp3 = row.musicMp3;
    },
    handleDelete(index, row) {
      console.log(index, row);
      deltete({id: row.id}).then(res => {
        this.GetList();
        this.$message({
          message: '恭喜你，删除成功',
          type: 'success'
        });
      })
    },
    currentChange() {
      console.log(this.currentPage);
      this.GetList();
    },
    GetList() {
      getList({ currentNum: this.currentPage, pageSize: 10}).then(res => {
        console.log(res);
        this.tableData = res.result.list;
        this.total = res.result.count;
      })
    },
    editMusic(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          editMusic({...this.form}).then(res => {
            this.dialogFormVisible = false;
            this.GetList();
            this.$message({
              message: '恭喜你，修改成功',
              type: 'success'
            });
          })
          this.dialogFormVisible = false;
        } else {
          console.log('error submit!!');
          return false;
        }
      });
      
    },
    addMusic(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addMusic({...this.form2}).then(res => {
            this.dialogFormVisibleImport = false;
            this.GetList();
            this.$message({
              message: '恭喜你，新增成功',
              type: 'success'
            });
          })
          this.dialogFormVisibleImport = false;
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  },
  created() {
    this.GetList();
  }
}
</script>

<style lang="scss" scoped>
  .SIMaintenance {
    .header {
      display: flex;
      align-items: flex-end;
      justify-content: flex-end;
    }
  }
</style>
